//
//  ChatWebViewController.m
//  AAIM
//
//  Created by awh on 2020/2/1.
//  Copyright © 2020年 AAIM. All rights reserved.
//

#import "ChatWebViewController.h"
#import <WebKit/WebKit.h>

@interface ChatWebViewController ()
<WKNavigationDelegate,WKScriptMessageHandler,WKUIDelegate,UIScrollViewDelegate,UIGestureRecognizerDelegate>

@property (nonatomic,strong)WKWebView * contentWebView;
@property(nonatomic,strong)UIProgressView *progressView;
@end

@implementation ChatWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.gk_navTitle = @"";
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    [self  createWebView];
//    self.gk_navigationItem.hidesBackButton = YES;
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setImage:[UIImage imageNamed:@"icon-返回键"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backacrion) forControlEvents:UIControlEventTouchUpInside];
    [backBtn sizeToFit];
    backBtn.frame = CGRectMake(0, 0, 44, 44);
    backBtn.jk_touchAreaInsets  = UIEdgeInsetsMake(10, 10, 10, 10);
//    markBackBtn = backBtn;
//    backBtn.hidden = YES;
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.gk_navLeftBarButtonItem = backItem;
    
    [self loadURL:self.urlStr];
}
-(void)loadURL:(NSString*)string
{
    NSString * str =[NSString stringWithFormat:@"%@",string];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:str]];
    [self.contentWebView loadRequest:request];
}
- (void)backacrion{
    if ([self.contentWebView canGoBack]) {
        /// 网页可以返回 就进行网页返回
        [self.contentWebView goBack];
    }else{
        
        [self.view resignFirstResponder];
        /// 网页返回到首页了 返回不了了 这时候我们的控制器返回
        [self.navigationController popViewControllerAnimated:YES];
    }
}
- (UIProgressView *)progressView
{
    if(!_progressView)
    {
        _progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, self.gk_navigationBar.bottom, self.view.frame.size.width , 15)];
        self.progressView.tintColor = KHEXCOLOR(kMainColor);
        self.progressView.trackTintColor = [UIColor whiteColor];
    }
    return _progressView;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if (object == self.contentWebView && [keyPath isEqualToString:@"estimatedProgress"]) {
        CGFloat newprogress = [[change objectForKey:NSKeyValueChangeNewKey] doubleValue];
        if (newprogress == 1) {
            
            self.progressView.hidden = YES;
            [self.progressView setProgress:0 animated:NO];
            
        }else {
            self.progressView.hidden = NO;
            [self.progressView setProgress:newprogress animated:YES];
        }
    }
}
-(WKWebView*)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures
{
    if (navigationAction.targetFrame.isMainFrame == NO) {
        [webView loadRequest:navigationAction.request];
    }
    return nil;
}
- (void)createWebView{
        _contentWebView = [[WKWebView alloc]initWithFrame:CGRectMake(0, self.gk_navigationBar.bottom, kScreenWidth, kScreenHeight-self.gk_navigationBar.bottom)];
        _contentWebView.UIDelegate = self;
        _contentWebView.navigationDelegate = self;
        [_contentWebView.scrollView setAlwaysBounceVertical:YES];
        _contentWebView.backgroundColor = [UIColor whiteColor];
        _contentWebView.scrollView.delegate=self;
        [_contentWebView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
        [self.view insertSubview:_contentWebView belowSubview:self.progressView];
       [self.view addSubview:_contentWebView];
       [self.view addSubview:self.progressView];
}

- (void)dealloc {
    [self.contentWebView removeObserver:self forKeyPath:@"estimatedProgress"];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
